﻿using Msg.Api.Models;
using OF.Component.BLL;
using OF.Component.Model;
using OF.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using OF.Utility.Logging;
using Msg.Api.Filters;

namespace Msg.Api.Controllers
{
    /// <summary>
    /// 发送短信
    /// </summary>
    public class SMSController : BaseApiController
    {
        #region 属性

        /// <summary>
        /// 车管在线短信接口地址
        /// </summary>
        private string CgSmsUrl
        {
            get
            {
                return SiteUtility.GetAppSettings("CG_SMSUrl");
            }
        }

        /// <summary>
        /// 车管在线短信备用接口地址
        /// </summary>
        private string CgSmsSpareUrl
        {
            get
            {
                return SiteUtility.GetAppSettings("CG_SMSUrl1");
            }
        }

        /// <summary>
        /// 车管在线短信账号
        /// </summary>
        private string CgSmsAccount
        {
            get
            {
                return SiteUtility.GetAppSettings("CG_SMSAccount");
            }
        }

        /// <summary>
        /// 车管在线短信密码
        /// </summary>
        private string CgSmsPassWord
        {
            get
            {
                return SiteUtility.GetAppSettings("CG_SMSPassWord");
            }
        }

        /// <summary>
        /// 斑马车务短信接口地址
        /// </summary>
        private string BmcwSmsUrl
        {
            get
            {
                return SiteUtility.GetAppSettings("BMCW_SMSUrl");
            }
        }

        /// <summary>
        /// 斑马车务短信备用接口地址1
        /// </summary>
        private string BmcwSmsSpareUrl1
        {
            get
            {
                return SiteUtility.GetAppSettings("BMCW_SMSUrl1");
            }
        }

        /// <summary>
        /// 斑马车务短信备用接口地址2
        /// </summary>
        private string BmcwSmsSpareUrl2
        {
            get
            {
                return SiteUtility.GetAppSettings("BMCW_SMSUrl2");
            }
        }

        /// <summary>
        /// 斑马车务账号
        /// </summary>
        private string BmcwSmsAccount
        {
            get
            {
                return SiteUtility.GetAppSettings("BMCW_SMSAccount");
            }
        }

        /// <summary>
        /// 斑马车务密码
        /// </summary>
        private string BmcwSmsPassWord
        {
            get
            {
                return SiteUtility.GetAppSettings("BMCW_SMSPassWord");
            }
        }

        /// <summary>
        /// 斑马车务短信接口地址(服务)
        /// </summary>
        private string BmcwSmsServiceUrl
        {
            get
            {
                return SiteUtility.GetAppSettings("BMCW_SMSUrl-Service");
            }
        }

        /// <summary>
        /// 斑马车务短信备用接口地址(服务)
        /// </summary>
        private string BmcwSmsServiceSpareUrl
        {
            get
            {
                return SiteUtility.GetAppSettings("BMCW_SMSUrl-Service1");
            }
        }

        /// <summary>
        /// 斑马车务短信账号(服务)
        /// </summary>
        private string BmcwSmsServiceAccount
        {
            get
            {
                return SiteUtility.GetAppSettings("BMCW_SMSAccount-Service");
            }
        }

        /// <summary>
        /// 斑马车务短信密码(服务)
        /// </summary>
        private string BmcwSmsServicePassWord
        {
            get
            {
                return SiteUtility.GetAppSettings("BMCW_SMSPassWord-Service");
            }
        }

        #endregion

        /// <summary>
        /// 发送短信
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [ActionCORS]
        public ResultData<bool> SendShortMessage(SmsModel model)
        {
            ResultData<bool> rData = new ResultData<bool>();
            string msg = "";
            bool isTrue = true;
            //斑马车务
            if (model.ChannelId == 1)
            {
                isTrue = BMCWSendMessage(model.Mobile, model.Content, out msg);
            }
            //车管在线
            if (model.ChannelId == 2)
            {
                isTrue = CGSendMessage(model.Mobile, model.Content, out msg);
            }
            RecordShortMessage(model.Mobile,model.Content,model.OpUserId,model.OpUserName,msg,model.SendTypeName,model.PlatformId, isTrue);
            if (!isTrue)
            {
                rData.status = -1;
            }
            rData.data = isTrue;
            rData.message = msg;
            return rData;
        }
     
        /// <summary>
        /// 批量发送短信 
        /// 暂时不支持车管在线调用
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [ActionCORS]
        public ResultData<bool> SendServiceShortMessage(SmsServiceModel model)
        {
            ResultData<bool> rData = new ResultData<bool>();
            string msg = "";
            SmsParam param = new SmsParam();
            param.Mobile = model.Mobile;
            param.Content = model.Content;
            param.Account = BmcwSmsServiceAccount;
            param.PassWord = BmcwSmsServicePassWord;
            param.SmsUrl = BmcwSmsServiceUrl;
            if (!SendSmsHelper.SendServiceMsg(param, out msg))
            {
                //如果发送失败，启用备用地址
                param.SmsUrl = BmcwSmsServiceSpareUrl;
                bool isTrue = SendSmsHelper.SendServiceMsg(param, out msg);
                if (!isTrue)
                {
                    rData.status = -1;
                    rData.message = msg;
                }
                rData.data = isTrue;
                return rData;
            }
            rData.data = true;
            return rData;
        }

        /// <summary>
        /// 发送验证码
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [ActionCORS]
        public ResultData<bool> SendCheckCode(CheckCodeModel model)
        {
            ResultData<bool> rData = new ResultData<bool>();
            string checkCode = "";
            string msgCheckCode = "";
            GetCheckCode(model.Mobile, model.PlatformId,out checkCode,out msgCheckCode);
            string msg = "";
            bool isTrue = true;
            //斑马车务
            if (model.ChannelId == 1)
            {
                isTrue = BMCWSendMessage(model.Mobile, msgCheckCode, out msg);
            }
            //车管在线
            if (model.ChannelId == 2)
            {
                isTrue = CGSendMessage(model.Mobile, msgCheckCode, out msg);
            }
            RecordShortMessage(model.Mobile, msgCheckCode, model.OpUserId, model.OpUserName, msg, model.SendTypeName, model.PlatformId, isTrue);
            if (!isTrue)
            {
                rData.status = -1;
                rData.message = string.Format("发送验证码失败,原因{0}", msg);
            }
            rData.data = isTrue;
            rData.message = msg;
            return rData;
        }

        /// <summary>
        /// 发送并返回验证码
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [ActionCORS]
        public ResultData<string> SendAndReturnCheckCode(CheckCodeModel model)
        {
            ResultData<string> rData = new ResultData<string>();
            string checkCode = "";
            string msgCheckCode = "";
            GetCheckCode(model.Mobile, model.PlatformId, out checkCode, out msgCheckCode);
            string msg = "";
            bool isTrue = true;
            //斑马车务
            if (model.ChannelId == 1)
            {
                isTrue = BMCWSendMessage(model.Mobile, msgCheckCode, out msg);
            }
            //车管在线
            if (model.ChannelId == 2)
            {
                isTrue = CGSendMessage(model.Mobile, msgCheckCode, out msg);
            }
            RecordShortMessage(model.Mobile, msgCheckCode, model.OpUserId, model.OpUserName, msg, model.SendTypeName, model.PlatformId, isTrue);
            if (!isTrue)
            {
                rData.status = -1;
                rData.message = string.Format("发送验证码失败,原因{0}", msg);
            }
            rData.data = OF.Utility.MD5Helper.MD5Key(checkCode);           
            return rData;
        }

        /// <summary>
        /// 判断验证码是否正确
        /// </summary>
        /// <param name="mobile"></param>
        /// <param name="verifyCode"></param>
        /// <param name="platformId"></param>
        /// <returns></returns>
        [HttpGet]
        [ActionCORS]
        public ResultData<bool> CheckVerificationCode(string mobile,string verifyCode,int platformId)
        {
            int typeId = 1;
            ResultData<bool> rData = new ResultData<bool>();
            rData.data = true;
            MsgVerificationCodeInfo verCodeInfo = MsgVerificationCodeOperate.GetByTypeAndMobile(typeId, mobile, platformId, verifyCode);
            if (verCodeInfo == null)
            {
                rData.data = false;
                rData.message = "验证码错误";
                rData.status = -1;
            }
            return rData;
        }

        /// <summary>
        /// 根据发送类型验证手机号是否发过信息
        /// </summary>
        /// <param name="mobile"></param>
        /// <param name="SendTypeName"></param>
        /// <param name="platformId"></param>
        /// <returns></returns>
        [HttpGet]
        [ActionCORS]
        public ResultData<bool> IsExistMobileByTypeName(string mobile, string SendTypeName, int platformId)
        {
            ResultData<bool> rData = new ResultData<bool>();
            try
            {
                MsgShortMessageInfo msgObj = MsgShortMessageOperate.QuerySingle(m => m.Mobile == mobile && m.TypeName == SendTypeName && m.PlatformId == platformId);
                if (msgObj == null)
                {
                    rData.data = false;
                    rData.message = "此手机号未发送信息";
                }
                else
                {
                    rData.data = true;
                }
            }
            catch (Exception ex)
            {
                rData.status = -1;
                rData.message = ex.Message;
            }
            return rData;
        }

        #region 公共方法

        /// <summary>
        /// 斑马车务短信发送
        /// </summary>
        /// <param name="mobile"></param>
        /// <param name="content"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        private bool BMCWSendMessage(string mobile, string content, out string msg)
        {
            msg = "";
            SmsParam param = new SmsParam();
            param.Mobile = mobile;
            param.Content = content;
            param.Account = BmcwSmsAccount;
            param.PassWord = BmcwSmsPassWord;
            param.SmsUrl = BmcwSmsUrl;
            if (!SendSmsHelper.SendMsg(param, out msg))
            {
                //如果发送失败，启用备用地址1
                param.SmsUrl = BmcwSmsSpareUrl1;
                if (!SendSmsHelper.SendMsg(param, out msg))
                {
                    //如果发送失败，启用备用地址2
                    param.SmsUrl = BmcwSmsSpareUrl2;
                    return SendSmsHelper.SendMsg(param, out msg);
                }
            }
            return true;

        }

        /// <summary>
        /// 车管在线短信发送
        /// </summary>
        /// <param name="mobile"></param>
        /// <param name="content"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        private bool CGSendMessage(string mobile, string content, out string msg)
        {
            msg = "";
            SmsParam param = new SmsParam();
            param.Mobile = mobile;
            param.Content = content;
            param.Account = CgSmsAccount;
            param.PassWord = CgSmsPassWord;
            param.SmsUrl = CgSmsUrl;
            if (!SendSmsHelper.SendMsg(param, out msg))
            {
                //如果发送失败，启用备用地址1
                param.SmsUrl = CgSmsSpareUrl;
                return SendSmsHelper.SendMsg(param, out msg);
            }
            return true;
        }

        /// <summary>
        /// 获取验证码
        /// </summary>
        /// <param name="mobile"></param>
        /// <param name="platformId"></param>
        /// <param name="checkCode"></param>
        /// <param name="msgContent"></param>
        private void GetCheckCode(string mobile, int platformId, out string checkCode, out string msgContent)
        {
            int typeId = 1;
            checkCode = "";
            msgContent = "您的手机验证码为：{0}。";
            MsgVerificationCodeInfo message = MsgVerificationCodeOperate.GetByMobileAndTypeId(typeId, mobile, platformId);
            if (message != null)
            {
                checkCode = message.CheckCode;
                msgContent = string.Format(msgContent, checkCode);
            }
            else
            {
                checkCode = ValidateNumber.GetRandomCheckCode();
                msgContent = string.Format(msgContent, checkCode);
                message = new MsgVerificationCodeInfo();
                message.Mobile = mobile;
                message.MsgDesc = msgContent;
                message.CheckCode = checkCode;
                message.TypeId = typeId;
                message.CreatedDate = DateTime.Now;
                message.ExpirationDate = DateTime.Now.AddMinutes(30);
                message.UpdatedDate = DateTime.Now;
                message.PlatformId = platformId;
                MsgVerificationCodeOperate.Insert(message);
            }

        }

        /// <summary>
        /// 记录发送日志
        /// </summary>
        /// <param name="mobile"></param>
        /// <param name="content"></param>
        /// <param name="opUserId"></param>
        /// <param name="opUserName"></param>
        /// <param name="msg"></param>
        /// <param name="sendTypeName"></param>
        /// <param name="platformId"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        private void RecordShortMessage(string mobile, string content, int opUserId, string opUserName, string msg, string sendTypeName, int platformId, bool result)
        {
            MsgShortMessageInfo info = new MsgShortMessageInfo
            {
                Mobile = mobile,
                Contents = content,
                CreatedDate = DateTime.Now,
                OpUserId = opUserId,
                OpUserName = opUserName,
                Result = result == true ? 1 : 0,
                ResultMsg = msg,
                TypeName = sendTypeName,
                PlatformId = platformId
            };
            MsgShortMessageOperate.Insert(info);
        }

        #endregion
    }
}